/**
 * @author Horváth Miklós 
 */

import greenfoot.*;

import java.util.Random;
import java.util.List;

public class KigyosVilag extends World
 {
  private static final int KIGYO = 0;
  private static final int ZAB = 1;
  private static final int KIGYOK_SZAMA = 5;
  private static final int ZABOK_SZAMA = 10;
 
  private int[][] kigyok_mentes = new int[KIGYOK_SZAMA][4];
  private int[][] zabok_mentes = new int[ZABOK_SZAMA][2];
  
  private GenAlg genalg;
  
  private Vombat vombat;
  
  public KigyosVilag() 
   {
    super(8, 8, 80);        
    setBackground("ret.jpg");
    kezdes();
   }
   
  public void kezdes()
   {
    genalg = new GenAlg();
    addObject(new Feszek(), 0, 0);
    addObject(new Vombat(genalg), 0, 0);
    addObject(new Malom(), 7, 7);
    random_elolenyek(KIGYOK_SZAMA, KIGYO);
    random_elolenyek(ZABOK_SZAMA, ZAB);
   }
   
  public void random_elolenyek(int mennyi, int faj)
   {
    int x;
    int y;
    int irany_x;
    int irany_y;
    if ((64 - numberOfObjects()) >= mennyi)
     {
      for (int i=0; i<mennyi; i++)
       {
        while (true)
         {
          x = Greenfoot.getRandomNumber(getWidth());
          y = Greenfoot.getRandomNumber(getHeight());
          List obj = getObjectsAt(x, y, Actor.class);
          if (obj.size() ==  0) break;
         }
        if (faj == KIGYO)
         {
          irany_x = Greenfoot.getRandomNumber(2) - 1;
          irany_y = Greenfoot.getRandomNumber(2) - 1;
          Kigyo kigyo = new Kigyo(irany_x, irany_y);
          kigyok_mentes[i][0] = x;
          kigyok_mentes[i][1] = y;
          kigyok_mentes[i][2] = irany_x;
          kigyok_mentes[i][3] = irany_y;
          addObject(kigyo, x, y); 
         }
        else if (faj == ZAB)
         {
          Zab zab = new Zab();
          zabok_mentes[i][0] = x;
          zabok_mentes[i][1] = y;
          addObject(zab, x, y); 
         }
       }
     }
   }
   
  public void mindent_torol()
   {
    int fitness;
    for (int i=0; i<8; i++)
     {
      for (int j=0; j<8; j++)
       {
        List obj = getObjectsAt(i, j, Actor.class);
        removeObjects(obj);
       }
     }
   }
     
  public void visszaallit()
   {
    addObject(new Feszek(), 0, 0);
    addObject(new Vombat(genalg), 0, 0);
    addObject(new Malom(), 7, 7);
    for (int i=0; i<KIGYOK_SZAMA; i++)
     {
      Kigyo kigyo = new Kigyo(kigyok_mentes[i][2], kigyok_mentes[i][3]);
      addObject(kigyo, kigyok_mentes[i][0], kigyok_mentes[i][1]);
     }
    for (int i=0; i<ZABOK_SZAMA; i++)
     {
      Zab zab = new Zab();
      addObject(zab, zabok_mentes[i][0], zabok_mentes[i][1]);
     }
   }
   
  public void repaint()
   {
    System.out.println("Tábla törlése");
    mindent_torol();
    System.out.println("Újrarajzolás");
    visszaallit();
   }
 }